package com.reserv.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.reserv.dao.MemberDAO;
import com.reserv.vo.MemberVO;

public class LoginServlet extends HttpServlet {
	private MemberDAO dao;
	@Override
	public void init(ServletConfig config) throws ServletException {
		dao = MemberDAO.getInstance();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//login status : 0-로그인성공, 1-비밀번호불일치 또는 아이디없음
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		String uid = request.getParameter("uid");
		String passwd = request.getParameter("passwd");
		
		MemberVO member = dao.getVO(uid);
		
		if((member == null) || !(member.getPasswd().equals(passwd))) {
			System.out.println("틀림!!");
			out.println("0");//아이디가 없거나 비밀번호가 없음
		} else {
			System.out.println("맞음!!");
			HttpSession session = request.getSession();
			session.setAttribute("member", member);
			session.setAttribute("isLogin", "true");
			out.println("1");
		}
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
}
